<template>
	<div class="login-container">
		<div class="login-box">
			<div class="logo-area">
				<span class="logo-text">荷包蛋的小店铺</span>
			</div>
			<h2>用户登录</h2>

			<el-form
				ref="loginForm"
				:model="formData"
				:rules="rules"
				@submit.prevent="handleSubmit"
			>
				<el-form-item prop="username">
					<el-input
						v-model="formData.username"
						:prefix-icon="User"
						placeholder="请输入用户名"
						size="large"
					/>
				</el-form-item>

				<el-form-item prop="password">
					<el-input
						v-model="formData.password"
						:prefix-icon="Lock"
						type="password"
						placeholder="请输入密码"
						size="large"
						show-password
					/>
				</el-form-item>

				<el-form-item prop="code" class="verification-container">
					<el-input
						v-model="formData.code"
						:prefix-icon="Key"
						placeholder="请输入验证码"
						size="large"
						class="verification-input"
					/>
					<div class="code-image" @click="refreshCode">
						<el-image :src="captchaUrl" fit="cover" />
					</div>
				</el-form-item>

				<el-form-item>
					<el-button
						type="primary"
						:loading="loading"
						size="large"
						class="submit-btn"
						@click="handleSubmit"
					>
						{{ loading ? '登录中...' : '登 录' }}
					</el-button>
				</el-form-item>
			</el-form>
		</div>
	</div>
</template>

<script setup>
import api from '@/request'
import { ElMessage } from 'element-plus'
import { User, Lock, Key } from '@element-plus/icons-vue'

const store = useStore()
const router = useRouter()

// 组件挂载时获取验证码
onMounted(() => {
	getCaptchaImage()
})

const loading = ref(false)
const loginForm = ref(null)

const formData = reactive({
	username: 'admin',
	password: '123456',
	code: ''
})

const rules = {
	username: [
		{ required: true, message: '请输入用户名', trigger: 'blur' },
		{ min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
	],
	password: [
		{ required: true, message: '请输入密码', trigger: 'blur' },
		{ min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
	],
	code: [
		{ required: true, message: '请输入验证码', trigger: 'blur' },
		{ len: 4, message: '验证码长度为 4 位', trigger: 'blur' }
	]
}

const captchaUrl = ref('')
const captchaCode = ref('')

// 获取验证码
const getCaptchaImage = async () => {
	try {
		const { data } = await api.getCaptcha()
		captchaUrl.value = data.image
		captchaCode.value = data.code
		formData.code = data.code
	} catch (error) {
		ElMessage.error('获取验证码失败')
	}
}

// 重写刷新验证码方法
const refreshCode = () => {
	formData.code = ''
	captchaCode.value = ''
	getCaptchaImage()
}

const handleSubmit = async () => {
	if (!loginForm.value || captchaCode.value !== formData.code) return

	try {
		loading.value = true
		await loginForm.value.validate()
		const { data } = await api.login(formData)
		localStorage.setItem('token', data.token)
		store.commit('setUser', data.userInfo)
		store.commit('setLogin', true)
		ElMessage.success('登录成功')
		router.push('/')
	} catch (error) {
		console.log(error)
		ElMessage.error('请检查输入信息')
	} finally {
		loading.value = false
	}
}
</script>

<style scoped>
.login-container {
	display: flex;
	justify-content: center;
	align-items: center;
	min-height: 100dvh;
	background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
	/* padding: 20px; */
}

.login-box {
	width: 420px;
	padding: 40px;
	background: rgba(255, 255, 255, 0.98);
	border-radius: 20px;
	box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
	backdrop-filter: blur(10px);
}

.logo-area {
	text-align: center;
	margin-bottom: 20px;
}

.logo-text {
	font-size: 32px;
	font-weight: bold;
	color: transparent;
	background: linear-gradient(45deg, #667eea, #764ba2);
	background-clip: text;
	-webkit-background-clip: text;
	display: inline-block;
	position: relative;
	padding: 0 10px;
	animation: logoFloat 3s ease-in-out infinite;
}

@keyframes logoFloat {
	0%,
	100% {
		transform: translateY(0);
	}
	50% {
		transform: translateY(-6px);
	}
}

.logo-text::after {
	content: '';
	position: absolute;
	bottom: -8px;
	left: 50%;
	transform: translateX(-50%);
	width: 100%;
	height: 4px;
	background: linear-gradient(45deg, #667eea, #764ba2);
	border-radius: 2px;
	opacity: 0.5;
}

h2 {
	text-align: center;
	margin-bottom: 20px;
	font-weight: 600;
	font-size: 24px;
	background: linear-gradient(45deg, #667eea, #764ba2);
	-webkit-background-clip: text;
	background-clip: text;
	color: transparent;
	padding: 0 10px;
}

.verification-container :deep(.el-form-item__content) {
	display: flex;
	gap: 12px;
}

.verification-input {
	flex: 1;
}

.code-image {
	width: 120px;
	height: 40px;
	border-radius: 8px;
	overflow: hidden;
	cursor: pointer;
	transition: transform 0.2s;
}

.code-image:hover {
	transform: scale(1.02);
}

.submit-btn {
	width: 100%;
	height: 44px;
	font-size: 16px;
	background: linear-gradient(45deg, #667eea, #764ba2);
	border: none;
	margin-top: 10px;
	transition: all 0.3s;
}

.submit-btn:hover {
	transform: translateY(-2px);
	box-shadow: 0 4px 12px rgba(102, 126, 234, 0.2);
}

.submit-btn:active {
	transform: translateY(0);
}

:deep(.el-input__wrapper) {
	box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
	border-radius: 8px;
	padding: 4px 12px;
}

:deep(.el-input__wrapper.is-focus) {
	box-shadow: 0 0 0 1px #667eea;
}

:deep(.el-input__inner) {
	height: 36px;
}

:deep(.el-form-item) {
	margin-bottom: 24px;
}

@media (max-width: 480px) {
	.login-box {
		width: 100%;
		padding: 30px 20px;
	}
}
</style>
